import random

# 定义武将类
class General:
    def __init__(self, name, health):
        self.name = name
        self.health = health
        self.hand_cards = []

    def draw_card(self, card):
        self.hand_cards.append(card)

    def play_card(self, card):
        if card in self.hand_cards:
            self.hand_cards.remove(card)
            return True
        return False


# 定义卡牌类
class Card:
    def __init__(self, name, effect):
        self.name = name
        self.effect = effect


# 初始化武将和卡牌
generals = [
    General("曹操", 4),
    General("刘备", 4),
    General("孙权", 4)
]

cards = [
    Card("杀", "对目标造成 1 点伤害"),
    Card("闪", "抵消杀的效果"),
    Card("桃", "回复 1 点体力")
]


# 选将函数
def select_general():
    print("可选武将：")
    for i, general in enumerate(generals, start=1):
        print(f"{i}. {general.name}（体力：{general.health}）")
    while True:
        try:
            choice = int(input("请选择一个武将编号 (1 - 3): "))
            if 1 <= choice <= 3:
                return generals[choice - 1]
            else:
                print("输入的编号无效，请重新输入。")
        except ValueError:
            print("输入无效，请输入一个有效的整数编号。")


# 发牌函数
def deal_cards(general, num_cards):
    for _ in range(num_cards):
        card = random.choice(cards)
        general.draw_card(card)
    print(f"{general.name} 获得了 {num_cards} 张牌，当前手牌：{[card.name for card in general.hand_cards]}")


# 出牌函数
def play_turn(attacker, defender):
    print(f"\n{attacker.name} 的回合：")
    print(f"当前手牌：{[card.name for card in attacker.hand_cards]}")
    while True:
        card_name = input("请输入要出的牌名（输入 '结束' 结束回合）：")
        if card_name == "结束":
            break
        for card in attacker.hand_cards:
            if card.name == card_name:
                if attacker.play_card(card):
                    if card.name == "杀":
                        print(f"{attacker.name} 对 {defender.name} 使用了 {card.name}")
                        dodge = input(f"{defender.name} 是否使用闪抵消？(y/n): ")
                        if dodge.lower() == 'y':
                            for d_card in defender.hand_cards:
                                if d_card.name == "闪":
                                    defender.play_card(d_card)
                                    print(f"{defender.name} 使用闪抵消了杀。")
                                    break
                            else:
                                print(f"{defender.name} 没有闪，受到 1 点伤害。")
                                defender.health -= 1
                        else:
                            print(f"{defender.name} 未使用闪，受到 1 点伤害。")
                            defender.health -= 1
                    elif card.name == "桃":
                        if attacker.health < attacker.max_health:
                            attacker.health += 1
                            print(f"{attacker.name} 使用桃回复了 1 点体力，当前体力：{attacker.health}")
                        else:
                            print(f"{attacker.name} 体力已满，无法使用桃。")
                            attacker.draw_card(card)
                    break
        else:
            print("你没有这张牌，请重新输入。")
    print(f"{attacker.name} 的回合结束。")
    print(f"{defender.name} 当前体力：{defender.health}")


# 主游戏函数
def main():
    print("欢迎来到简化版三国杀游戏！")
    player = select_general()
    computer = random.choice([g for g in generals if g != player])
    print(f"你选择了 {player.name}，电脑选择了 {computer.name}")

    # 初始发 4 张牌
    deal_cards(player, 4)
    deal_cards(computer, 4)

    while player.health > 0 and computer.health > 0:
        play_turn(player, computer)
        if computer.health <= 0:
            print(f"{computer.name} 被击败，你获胜了！")
            break
        play_turn(computer, player)
        if player.health <= 0:
            print(f"{player.name} 被击败，你输了！")
            break


if __name__ == "__main__":
    main()